import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/match/detail/zq_match_odds_detail_logic.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/record/items/data_left_list.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';

import '../../../widgets/base/qkd_text.dart';

class ZqMatchOddsDetailView extends StatelessWidget {
  ZqMatchOddsDetailView({super.key});

  final logic = Get.put(ZqMatchOddsDetailLogic());
  @override
  Widget build(BuildContext context) {
    return GetBuilder<ZqMatchOddsDetailLogic>(builder: (_) {
      return Scaffold(
          backgroundColor: Colours.main,
          appBar: Styles.appBar(
            backgroundColor: Colours.main,
            titleTextStyle: const TextStyle(
                fontSize: 14, color: Colours.white
            ),
            title: SizedBox(
              width: MediaQuery.of(context).size.width - 140,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: QkdText(
                      logic.detail.info?.baseInfo?.homeName ?? "",
                    ).alignEnd
                  ),
                  QkdText(" vs "),
                  Expanded(
                    child: QkdText(
                      logic.detail.info?.baseInfo?.guestName ?? "",
                    ).alignStart
                  )
                ]
              ),
            ),
            leadingColor: Colours.white
          ),
          body: Container(
            clipBehavior: Clip.antiAlias,
            decoration: const BoxDecoration(
                color: Colours.white,
                borderRadius: BorderRadius.only(
                    topRight: Radius.circular(10),
                    topLeft: Radius.circular(10))),
            child: LoadingWidget<int>(
              isLoading: false,
              data: logic.companyList[logic.typeIndex].length,
              child: () => Row(
                children: [
                  Container(
                    padding: const EdgeInsets.only(top: 16),
                    color: logic.selectIndex[logic.typeIndex] == 0
                        ? Colours.white
                        : Colours.greyF5F7FA,
                    child: DataLeftList(
                        logic.companyList[logic.typeIndex]
                            .map((e) => e.name ?? "")
                            .toList(),
                        logic.selectIndex[logic.typeIndex], (p0) {
                      logic.setSelectIndex(logic.typeIndex, p0);
                    }),
                  ),
                  Expanded(child: rightPage(logic.typeIndex))
                ],
              ),
            ),
          ));
    });
  }

  Widget rightPage(int i) {
    return Column(
      children: [
        DefaultTextStyle(
          style: TextStyle(
            fontSize: 12,color: Colours.grey66
          ),
          child: Container(
            height: 40,
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Row(
              children: [
                Expanded(
                    flex: 2,
                    child: QkdText(logic.headTypeList[i][0]).center
                ),
                Expanded(
                    flex: 2,
                    child: QkdText(logic.headTypeList[i][1]).center
                ),
                Expanded(
                    flex: 2,
                    child: QkdText(logic.headTypeList[i][2]).center
                ),
                Expanded(
                    flex: 3,
                    child: QkdText("时间").center
                )
              ],
            ),
          ),
        ),
        Expanded(
          child: LoadingWidget<int>(
            isLoading: false,
            data: logic.data[i].length,
            child: () => SingleChildScrollView(
                physics: const ClampingScrollPhysics(),
                child: Column(
                  children: List.generate(logic.data[i].length, (index) {
                    var entity = logic.data[i][index];
                    var data =
                        logic.formSingleLine(logic.data[i][index].data ?? []);
                    return Stack(
                      children: [
                        DefaultTextStyle(
                          style: TextStyle(fontSize: 12),
                          child: Container(
                            height: 44,
                            margin: const EdgeInsets.symmetric(horizontal: 16),
                            decoration: const BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        color: Colours.greyEE, width: 0.5))),
                            child: Row(
                              children: [
                                Expanded(
                                  flex: 2,
                                  child: (entity.closed == "封"
                                    ? QkdText("-") : data[0]
                                  ).center
                                ),
                                Expanded(
                                  flex: 2,
                                  child: (i == 1 || i == 3
                                    ? QkdText(entity.line ?? "")
                                    : entity.closed == "封"
                                      ? QkdText("封")
                                      : data[1]
                                  ).center
                                ),
                                Expanded(
                                  flex: 2,
                                  child: (entity.closed == "封"
                                    ? QkdText("-") : data[2]
                                  ).center
                                ),
                                Expanded(
                                  flex: 3,
                                  child: QkdText(
                                    entity.updateTime.noValue == true ? "" :
                                    DateTime.parse(entity.updateTime!)
                                      .formatedString("MM/dd HH:mm"),
                                    color: Colours.grey99
                                  ).center
                                )
                              ],
                            ),
                          ),
                        ),
                        if (entity.beginFlag == true)
                          Align(
                              alignment: Alignment.topCenter,
                              child: _beginFlag())
                      ],
                    );
                  }),
                ).paddingOnly(bottom: 50)),
          ),
        )
      ],
    );
  }

  Widget _beginFlag() {
    return Container(
      height: 14,
      width: 36,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
          color: Colours.main,
          borderRadius: BorderRadius.vertical(bottom: Radius.circular(4))),
      child: QkdText("赛前", color: Colours.white, fontSize: 10),
    );
  }
}
